package com.cn.por.auto.factory;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;

import com.cn.por.auto.dto.DataModel;
import com.cn.por.auto.dto.FunctionMessage;
import com.cn.por.auto.dto.ModelParam;
import com.cn.por.auto.enums.FunctionName;
import com.cn.por.util.freemarker.StringFormaterUtil;

public class ControllerDataModelFactory extends InterfaceDataModelFactory {
	public ControllerDataModelFactory() {}
	@Override
	public void setProperties(ModelParam modelParam) {
		super.setProperties(modelParam);
		String injectClass = super.interfaceDataModel.getEntityName()+"Service";
		interfaceDataModel.setInjectClass(injectClass);
		interfaceDataModel.getImportPackage().add("org.springframework.web.bind.annotation.RequestBody");
		interfaceDataModel.getImportPackage().add("org.springframework.web.bind.annotation.RequestMapping");
		interfaceDataModel.getImportPackage().add("org.springframework.web.bind.annotation.RestController");
		interfaceDataModel.getImportPackage().add("org.springframework.beans.factory.annotation.Autowired");
		interfaceDataModel.getImportPackage().add(modelParam.getBasePackage()+".service."+interfaceDataModel.getEntityName()+"Service");
		List<String> annotations = new ArrayList<String>();
		annotations.add("RestController");
		annotations.add("RequestMapping(\"/web/"+StringFormaterUtil.lowerCaptureName(interfaceDataModel.getEntityName())+"\")");
		for(FunctionMessage fun : interfaceDataModel.getFunctionMsgs()) {
			fun.setAnnotation("@RequestMapping(\"/"+fun.getFunctionName()+"\")");
			if(!fun.getFunctionName().endsWith("Id")) {
				fun.setParamAnnotation("@RequestBody");
			}
		}
		interfaceDataModel.setAnnotations(annotations);
		interfaceDataModel.setFolder(interfaceDataModel.getPackageName().replace(".", "/"));
	}
	
	@Override
	public DataModel getDataModel() {
		return super.getDataModel();
	}

}
